using UnityEngine;
using System.Collections;

[System.Serializable]
public class ChangeLevelOnTrigger : MonoBehaviour {
	
	public string level = "";
	public bool onKeyUse = true;
	
	public bool debug = false;
	
	private bool keyUsePressed = false;
	private bool triggered = false;
	
	// Use this for initialization
	void Start () {
	
	}
	
	void OnTriggerEnter(Collider collision) {
		if (collision.tag == "Player") {
			triggered = true;
        }
	}
	
	void OnTriggerExit(Collider collision) {
		if (collision.tag == "Player") {
			triggered = false;
        }
	}
	
	private void loadLevel() {
		Screen.lockCursor = false;
		Screen.showCursor = true;
		Application.LoadLevel(level);
	}
	
	void Update() {
		keyUsePressed = Input.GetKey("e");
			
		if (triggered) {
		   	if (onKeyUse) {
				if (keyUsePressed) {
					loadLevel();	
				}
			} else {
				loadLevel();	
			}
		}
	}
	
	void OnGUI() {
		if (debug) {
			GUI.Box(new Rect(1,1, 200, 200), "Triggered: " + triggered + "\n" + "Use key pressed: " + keyUsePressed);
		}
	}
}
